package com.engc.common.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.engc.common.dao.DBExecuteException;
import com.engc.common.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Resource
	private JdbcTemplate jdbcTemplate;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getUsercode(final String areano, final Integer usertype) throws DBExecuteException {

		final String sql = "{? = call f_comm_getusercode(?,?)}";
		Object obj = this.jdbcTemplate.execute(new ConnectionCallback() {
			public Object doInConnection(Connection conn) throws SQLException, DataAccessException {

				CallableStatement cstmt = conn.prepareCall(sql);

				cstmt.registerOutParameter(1, Types.VARCHAR);
				cstmt.setObject(2, areano);
				cstmt.setObject(3, usertype);

				cstmt.execute();
				String out = cstmt.getString(1);
				cstmt.close();

				return out;
			}
		});
		if (obj != null && !obj.toString().trim().equals("")) {
			return obj.toString();
		}
		return null;

	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public String getTempAdminId(String idtype) {

		if (StringUtils.isBlank(idtype)) {
			return "";
		}
		String prefix = null;
		Long seq = jdbcTemplate.queryForLong("select SEQ_ADMINTEMP_ID.nextval from dual ");
		if (idtype.equals("1")) {
			// 学生录入账户
			prefix = "studentLr";
		} else if (idtype.equals("2")) {
			// 教师录入账户
			prefix = "teacherLr";
		} else if (idtype.equals("3")) {
			// 班主任账户
			prefix = "bzrAdmin";
		} else if (idtype.equals("4")) {
			// 年级账户
			prefix = "njzrAdmin";
		} else if (idtype.equals("5")) {
			// 学校账户
			prefix = "schoolAdmin";
		} else {
			prefix = "etkAdmin";
		}
		return prefix + seq;
	}

	

}
